package com.api.shopapi.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.api.shopapi.entity.*;
import com.api.shopapi.entity.dto.MedicationGroupDTO;
import com.api.shopapi.service.InstitutionStaffRelService;
import com.api.shopapi.service.MedicationGroupService;
import com.api.shopapi.service.MedicationItemService;
import com.api.shopapi.service.StaffService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;

/**
 * @author maochuan
 * 用药组
 */
@RestController
@RequestMapping("MedicationGroup")
public class MedicationGroupController extends BaseController {

    @Autowired
    @Qualifier("MedicationGroupServiceImplOne")
    private MedicationGroupService medicationGroupService;

    @Autowired
    @Qualifier("MedicationItemServiceImplOne")
    private MedicationItemService medicationItemService;

    @Autowired
    @Qualifier("StaffServiceImplOne")
    private StaffService staffService;


    @Autowired
    @Qualifier("InstitutionStaffRelServiceImplOne")
    private InstitutionStaffRelService institutionStaffRelService;


    @RequestMapping(value = "Add", method = RequestMethod.POST)
    public MyResult Insert(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");
            String groupName = jsonObject.getString("groupName");
            String elderlyCode = jsonObject.getString("elderlyCode");
            JSONArray itemNameArr = jsonObject.getJSONArray("itemNameArr");
            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode,"/MedicationGroup/Add");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffCode);
            String institutionCode = institutionStaffRel.getInstitutionCode();

            MedicationGroup medicationGroup = medicationGroupService.getByElderlyCode(elderlyCode);
            if(medicationGroup!=null){
                return failed("此老人用药组已存在");
            }
            if(itemNameArr.size()==0){
                return failed("此用药组的项为空");
            }
            medicationGroup = new MedicationGroup();
            medicationGroup.setMedicationGroupCode(UUID.randomUUID().toString());
            medicationGroup.setName(groupName);
            medicationGroup.setElderlyCode(elderlyCode);
            medicationGroup.setStaffCode(staffCode);
            medicationGroup.setInstitutionCode(institutionCode);

            medicationGroupService.insert(medicationGroup,itemNameArr);
            Object data = new Object[]{};
            return success(data);
        } catch (Exception e) {
            insertError("/MedicationGroup/Add", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }

    @RequestMapping(value = "Edit", method = RequestMethod.POST)
    public MyResult update(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");
            String medicationGroupCode = jsonObject.getString("medicationGroupCode");
            String elderlyCode = jsonObject.getString("elderlyCode");
            String groupName = jsonObject.getString("groupName");
            int status = jsonObject.getInteger("status");
            JSONArray itemNameArr = jsonObject.getJSONArray("itemNameArr");
            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode,"/MedicationGroup/Edit");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            if(itemNameArr.size()==0){
                return failed("此用药组的项为空");
            }

            MedicationGroup medicationGroup = medicationGroupService.getByElderlyCode(elderlyCode);
            if(medicationGroup!=null){
                if(!medicationGroup.getMedicationGroupCode().equals(medicationGroupCode)){
                    return failed("此用药组已存在");
                }
            }else {
                medicationGroup = medicationGroupService.getByMedicationGroupCode(medicationGroupCode);
            }

            medicationGroup.setName(groupName);
            medicationGroup.setElderlyCode(elderlyCode);
            medicationGroup.setStatus(status);
            medicationGroupService.updateByMedicationGroupCode(medicationGroup,itemNameArr);

            Object data = new Object[]{};
            return success(data);
        }catch (Exception e) {
            insertError("/MedicationGroup/Edit", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }

    @RequestMapping(value = "Page", method = RequestMethod.POST)
    public MyResult page(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String elderlyName = jsonObject.getString("elderlyName");
            int pageIndex = jsonObject.getInteger("pageIndex");
            int status = jsonObject.getInteger("status");
            int pageSize = jsonObject.getInteger("pageSize");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode,"/MedicationGroup/Page");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffCode);
            String institutionCode = institutionStaffRel.getInstitutionCode();

            PageInfo<MedicationGroupDTO> medicationGroupDTOPageInfo = medicationGroupService.getPageList(pageIndex, pageSize, institutionCode,status,elderlyName);
            for (MedicationGroupDTO medicationGroupDTO:medicationGroupDTOPageInfo.getList()){
                List<MedicationItem> medicationItemList = medicationItemService.getByMedicationGroupCode(medicationGroupDTO.getMedicationGroupCode());
                medicationGroupDTO.setMedicationItemList(medicationItemList);
            }

            return success(medicationGroupDTOPageInfo);
        }catch (Exception e) {
            insertError("/MedicationGroup/Page", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }


    @RequestMapping(value = "GovPage", method = RequestMethod.POST)
    public MyResult GovPage(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String elderlyName = jsonObject.getString("elderlyName");
            int pageIndex = jsonObject.getInteger("pageIndex");
            int status = jsonObject.getInteger("status");
            int pageSize = jsonObject.getInteger("pageSize");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode,"/MedicationGroup/Page");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            Staff staff = staffService.getByStaffCode(staffCode);

            PageInfo<MedicationGroup> medicationGroupDTOPageInfo = medicationGroupService.pageByGovCode(pageIndex, pageSize, staff.getGovernmentCode());

            List<MedicationGroupDTO> listDTO = new ArrayList<>();

            for (MedicationGroup medicationGroup:medicationGroupDTOPageInfo.getList()){

                MedicationGroupDTO medicationGroupDTO = mapper.map(medicationGroup,MedicationGroupDTO.class);

                List<MedicationItem> medicationItemList = medicationItemService.getByMedicationGroupCode(medicationGroup.getMedicationGroupCode());
                medicationGroupDTO.setMedicationItemList(medicationItemList);

                listDTO.add(medicationGroupDTO);

            }

            medicationGroupDTOPageInfo.setList(Collections.unmodifiableList(listDTO));

            return success(medicationGroupDTOPageInfo);
        }catch (Exception e) {
            insertError("/MedicationGroup/Page", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }


}
